package cn.yunhe.crm.annotation;


import java.lang.reflect.Method;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.yunhe.crm.entity.Emp;
import cn.yunhe.crm.entity.SysLog;
import cn.yunhe.crm.service.SysLogService;



@Aspect
@Component
public class SystemLogAspect {
	
	@Autowired
	@Qualifier("syslogService")
	private SysLogService syslogservice;
	
	public void setSyslogbiz(SysLogService syslogservice) {
		this.syslogservice = syslogservice;
	}

	//Controller层切点     
    @Pointcut("@annotation(cn.yunhe.crm.annotation.SystemLog)")  
    public void controllerAspect(){
    	
    } 
    
    @AfterReturning("controllerAspect()")
    public void doBefore(JoinPoint joinPoint){
    	 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();   
         HttpSession session = request.getSession();
         //读取session中的用户
         Emp user=(Emp) session.getAttribute("user");
         //String ip=request.getRemoteAddr();
         try {
			String des=getControllerMethodDescription(joinPoint);
			SysLog syslog=new SysLog();
			syslog.setSessionid(session.getId());
			syslog.setEmpname(user.getEmpname());
			syslog.setUsername(user.getUsername());
			syslog.setDeptname(user.getDept().getName());
			syslog.setIp(request.getRemoteAddr());
			syslog.setAction(des);
			syslog.setLogintime((Date) session.getAttribute("logintime"));
			syslog.setActiontime(new Date());
			syslogservice.add(syslog);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
         
         
    }
    
    
    /**    
     * 获取注解中对方法的描述信息 用于Controller层注解    
     *    
     * @param joinPoint 切点    
     * @return 方法描述    
     * @throws Exception    
     */      
     public  static String getControllerMethodDescription(JoinPoint joinPoint)  throws Exception {      
        String targetName = joinPoint.getTarget().getClass().getName();    //获得执行方法的类名    
        String methodName = joinPoint.getSignature().getName();            //获得执行方法的方法名  
        Object[] arguments = joinPoint.getArgs();                          //获取切点方法的所有参数类型  
        Class targetClass = Class.forName(targetName);      
        Method[] methods = targetClass.getMethods();    //获取公共方法，不包括类私有的  
        String description = "";      
         for (Method method : methods) {      
             if (method.getName().equals(methodName)) {      
                Class[] clazzs = method.getParameterTypes();     //对比方法中参数的个数      
                 if (clazzs.length == arguments.length) {      
                    description = method.getAnnotation(SystemLog.class).des();      
                     break;      
                }      
            }      
        }      
         return description;      
    }  
    
}
   